//
//  HETouchIDViewController.m
//  HealthFamily
//
//  Created by 吴欧 on 2021/2/23.
//  Copyright © 2021 honglutec. All rights reserved.
//

#import "HETouchIDViewController.h"
#import "HETouchID.h"
#import "HESecurtyChainData.h"

@interface HETouchIDViewController ()

@property (nonatomic, strong) UIImageView *userIcon;
@property (nonatomic, strong) UILabel *usernameLb;
@property (nonatomic, strong) QMUIButton *actionBtn;
@property(nonatomic , strong) UIButton *smsLoginBtn;
@property(nonatomic , assign) HETouchIDSupperType supperType;

@end

@implementation HETouchIDViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.zx_navLineView.hidden = YES;
    
    _supperType = [[HETouchID sharedInstance] td_canSupperBiometrics];
    self.title = _supperType == HETouchIDSupperTypeTouchID ? @"指纹登录" :
                 _supperType == HETouchIDSupperTypeFaceID ? @"面容登录" : @"";
    
    [self initUI];
}

- (void)initUI {
    self.smsLoginBtn.hidden = NO;
    
    HESecuryChainUserModel *user = [HESecurtyChainData getFingerprintPWD];
    [self.userIcon sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:IMAGE_NAMED(@"defualt_icon")];
    self.usernameLb.text = [NSString desensitizationWithPhone:user.username] ;
    
    [self.view addSubview:self.userIcon];
    [self.view addSubview:self.usernameLb];
    [self.view addSubview:self.actionBtn];

    
    [self.userIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(ZXNavBarHeight + 40);
        make.width.height.offset(80);
        make.centerX.equalTo(self.view);
    }];
    
    [self.usernameLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userIcon.mas_bottom).offset(30);
        make.height.offset(20);
        make.centerX.equalTo(self.view);
    }];
    
    [self.actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.usernameLb.mas_bottom).offset(50);
//        make.height.offset(180);
        make.centerX.equalTo(self.view);
    }];
}

- (void)toAuthClick {
    [[HETouchID sharedInstance] td_showTouchIDWithDescribe:nil BlockState:^(HETouchIDState state, NSError *error) {
        if (state == HETouchIDStateSuccess) {    //TouchID/FaceID验证成功
            [[UserManager sharedUserManager] reloadMain];
        }
    }];
}

#pragma mark -

- (UIImageView *)userIcon {
    if (!_userIcon) {
        _userIcon = [UIImageView new];
        _userIcon.layer.masksToBounds = YES;
        _userIcon.layer.cornerRadius = 40;
    }
    return _userIcon;
}

- (UILabel *)usernameLb {
    if (!_usernameLb) {
        _usernameLb = [UILabel new];
        _usernameLb.font = DYFont(16);
    }
    return _usernameLb;
}

- (QMUIButton *)actionBtn {
    if (!_actionBtn) {
        _actionBtn = [[QMUIButton alloc] init];
        _actionBtn.imagePosition = QMUIButtonImagePositionTop;// 将图片位置改为在文字下方
        _actionBtn.adjustsImageTintColorAutomatically = NO;
        _actionBtn.spacingBetweenImageAndTitle = 20;
        
        if (_supperType == HETouchIDSupperTypeTouchID) {
            [_actionBtn setImage:IMAGE_NAMED(@"touchID") forState:UIControlStateNormal];
            [_actionBtn setTitle:@"点击进行指纹登录" forState:UIControlStateNormal];

        }else {
            [_actionBtn setImage:IMAGE_NAMED(@"faceID") forState:UIControlStateNormal];
            [_actionBtn setTitle:@"点击进行面容登录" forState:UIControlStateNormal];
        }
        
        [_actionBtn setTitleColor:KHexColor(@"#1296db") forState:UIControlStateNormal];
        [_actionBtn addTarget:self action:@selector(toAuthClick) forControlEvents:UIControlEventTouchUpInside];
        _actionBtn.titleLabel.font = DYBlodFont(16);
    }
    return _actionBtn;
}

- (UIButton *)smsLoginBtn {
    if (!_smsLoginBtn) {
        _smsLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_smsLoginBtn setTitle:@"手机短信登录" forState:UIControlStateNormal];
        [_smsLoginBtn setTitleColor:KHexColor(@"#1296db") forState:UIControlStateNormal];
        _smsLoginBtn.titleLabel.font = DYFont(16);
        _smsLoginBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_smsLoginBtn addTarget:self action:@selector(toSmsLoginClick) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_smsLoginBtn];
        [_smsLoginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.view).offset(-(30 + ZXSafeAreaBottom));
            make.centerX.equalTo(self.view);
        }];
        self.smsLoginBtn.hidden = YES;
    }
    return _smsLoginBtn;
}

- (void)toSmsLoginClick {
    HELoginViewController *vc = [HELoginViewController new];
    [self presentViewController:vc animated:YES completion:nil];
}

@end
